package io.renren.dao;

import io.renren.common.dao.BaseDao;
import org.apache.ibatis.annotations.Mapper;
import io.renren.entity.AutorescueitemsdEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-29
 */
@Mapper
public interface AutorescueitemsdDao extends BaseDao<AutorescueitemsdEntity> {

    @Select("SELECT BillDate,Fk,typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,TotalAmount,Amount,RowNum,BillCode " +
            "FROM ( " +
            "SELECT BillDate,case when BillType='AAR' and RepairBusinessType like '%保养%' then '车辆保养' when BillType='AAR' and RepairBusinessType is null or RepairBusinessType not like '%保养%' then '车辆维修' else '车辆返修' end as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,RepairTotalAmount as TotalAmount,RepairAmount as Amount,AutoRepairMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM AutoRepairM ${whereSql} " +
            "union All " +
            "SELECT BillDate,'车辆美容' as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,RepairTotalAmount as TotalAmount,RepairAmount as Amount,AutoDecorateMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM AutoDecorateM ${whereSql} " +
            "UNION all " +
            "SELECT BillDate,'车辆洗车' as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,TotalAmount as TotalAmount,Amount as Amount,AutoWashMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM AutoWashM ${whereSql} " +
            "UNION all " +
            "SELECT BillDate,'车辆救援' as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,BusinessAmount as TotalAmount,(BusinessAmount - DiscountAmount) as Amount,AutoRescueMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM AutoRescueM  ${whereSql} " +
            "UNION all " +
            "SELECT BillDate,'保险' as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,InsuranceTotalAmount as TotalAmount,Amount as Amount,AgencyInsuranceRegMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM AgencyInsuranceRegM  ${whereSql} " +
            "UNION all " +
            "SELECT BillDate,'车辆VIP' as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,MembershipAmount as TotalAmount,MembershipAmount as Amount,JoinMemberMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM JoinMemberM  ${whereSql} " +
            "UNION all " +
            "SELECT BillDate,'车务' as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,Cost as TotalAmount,Amount as Amount,AutoServiceMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM AutoServiceM  ${whereSql} " +
            ") AS SubQuery " +
            "WHERE RowNum >= ${page} AND RowNum <= ${perPage} order by BillDate desc,BillCode desc ")
    List<Map<String, Object>> selectMapBySql(@Param("whereSql") String whereSql, @Param("page") Integer page, @Param("perPage") Integer perPage);

    @Select("SELECT count(*) as num " +
            "FROM ( " +
            "SELECT BillDate,case when BillType='AAR' and RepairBusinessType like '%保养%' then '车辆保养' when BillType='AAR' and RepairBusinessType is null or RepairBusinessType not like '%保养%' then '车辆维修' else '车辆返修' end as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,RepairTotalAmount as TotalAmount,RepairAmount as Amount,AutoRepairMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM AutoRepairM ${whereSql} " +
            "union All " +
            "SELECT BillDate,'车辆美容' as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,RepairTotalAmount as TotalAmount,RepairAmount as Amount,AutoDecorateMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM AutoDecorateM ${whereSql} " +
            "UNION all " +
            "SELECT BillDate,'车辆洗车' as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,TotalAmount as TotalAmount,Amount as Amount,AutoWashMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM AutoWashM ${whereSql} " +
            "UNION all " +
            "SELECT BillDate,'车辆救援' as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,BusinessAmount as TotalAmount,(BusinessAmount - DiscountAmount) as Amount,AutoRescueMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM AutoRescueM  ${whereSql} " +
            "UNION all " +
            "SELECT BillDate,'保险' as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,InsuranceTotalAmount as TotalAmount,Amount as Amount,AgencyInsuranceRegMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM AgencyInsuranceRegM  ${whereSql} " +
            "UNION all " +
            "SELECT BillDate,'车辆VIP' as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,MembershipAmount as TotalAmount,MembershipAmount as Amount,JoinMemberMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM JoinMemberM  ${whereSql} " +
            "UNION all " +
            "SELECT BillDate,'车务' as typeName,SYS_Created,Org_FK,Auto_FK,UCML_CONTACT_FK,Cost as TotalAmount,Amount as Amount,AutoServiceMOID as Fk, ROW_NUMBER() OVER (ORDER BY SYS_Created desc) AS RowNum,BillCode FROM AutoServiceM  ${whereSql} " +
            ") AS SubQuery ")
    Integer selectCountBySql(String whereSql);

    @Select("select b.Name as projectName,b.Name,a.Amount,a.DiscountAmount,a.BusinessAmount from Autorescueitemsd a left join ProjectM b ON a.ProjectM_FK = b.ProjectMOID " +
            "where AutoRescueM_FK =#{autoRescueMoId}")
    List<Map<String, Object>> getProjectList(@Param("autoRescueMoId") String autoRescueMoId);

    @Select("select Name  from AutoRescueGoodsD where AutoRescueM_FK =#{Fk}")
    List<Map<String, String>> getProjectListForAutorescuegoodsd(@Param("Fk") String Fk);

    @Select("select GoodsM_FK from AutoRepairPickingD where AutoRepairM_FK =#{Fk}")
    List<Map<String, String>> findGoodsMOIDForAutorepairpickingd(@Param("Fk") String Fk);
}